﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DbAdapter.sqldbAdapter;

namespace DAO.WaiGaoQiao
{
    public class DataAccessCon_Goods
    {
        /// <summary>
        /// 获取所有货类型。
        /// </summary>
        /// <returns></returns>
        public DataSet GetCon_GoodsById(int Id)
        {
            sqldb db = new sqldb();
            string query = "select ID,GoodsNo,GoodsName,GoodsUnit,PriorID,Type from Con_Goods where 1=1 and Id = " + Id;
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }


        /// <summary>
        /// 获取所有货类型。
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllCon_Goods(string condition)
        {
            sqldb db = new sqldb();
            string query = "select ID,GoodsNo,GoodsName,GoodsUnit,PriorID from Con_Goods where 1=1 " + condition;
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 获取所有(分页）
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllCon_Goods(string Condition, int pagesize, int startindex)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select B.ID,B.GoodsNo,B.GoodsName,GoodsUnit,B.PriorID from (select ROW_NUMBER() OVER(ORDER BY A.ID DESC) rownumber,"
                             + "A.ID,A.GoodsNo,A.GoodsName,GoodsUnit,A.PriorID from Con_Goods A where 1=1 " + Condition + ") B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                return db.ExecuteDataset(conn, CommandType.Text, query);
            }
        }

        /// <summary>
        /// 保存或修改食材类型
        /// </summary>
        /// <param name="dmpara"></param>
        /// <returns></returns>
        public int SaveCon_Goods(object[] goodsType)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                SqlParameter[] parameters = {
                    new SqlParameter("@ID", SqlDbType.Int),
                    new SqlParameter("@GoodsNo", SqlDbType.VarChar,50),
                    new SqlParameter("@GoodsName",SqlDbType.VarChar,50),
                    new SqlParameter("@GoodsUnit", SqlDbType.VarChar,20),
                    new SqlParameter("@PriorID", SqlDbType.Int),
                    new SqlParameter("@Type", SqlDbType.VarChar,10)
                    };
                parameters[0].Value = goodsType[0];
                parameters[1].Value = goodsType[1];
                parameters[2].Value = goodsType[2];
                parameters[3].Value = goodsType[3];
                parameters[4].Value = goodsType[4];
                parameters[5].Value = goodsType[5];
                return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.StoredProcedure, "Proc_Con_Goods", parameters);


                //return db.ExecuteNonQuery(conn, "Proc_Con_Goods", goodsType);
            }
        }


        /// <summary>
        /// 删除食材类型
        /// </summary>
        /// <param name="did"></param>
        /// <returns></returns>
        public int DeleteCon_Goods(string id)
        {
            sqldb db = new sqldb();
            string query = "delete from Con_Goods where  id = " + id;
            return db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 食材类型数量合计
        /// </summary>
        /// <returns></returns>
        public int GetCon_GoodsCount(string Condition)
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Con_Goods where 1=1 " + Condition;
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }
        /// <summary>
        /// 仓库中的食材数量合计
        /// </summary>
        /// <param name="Condition"></param>
        /// <returns></returns>
        public int GetCon_Goods_StocksCount(string Condition)
        {
            sqldb db = new sqldb();
            string query = "select count(1) from CON_Goods_Stocks_View where 1=1 " + Condition;
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }
        /// <summary>
        /// 获取仓库中的食材
        /// </summary>
        /// <param name="Condition"></param>
        /// <returns></returns>
        public DataSet GetCon_Goods_StocksList(string Condition)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select b.ID,a.* from CON_Goods_Stocks_View a left join con_goods b on " +
                               "a.ID=b.ID where 1=1 and a.STOCKS_COUNT>0 " +
                               "and a.GOODSNO in( select GoodsNo from Con_Goods where PriorID " +
                               "not in(select ID from Con_Goods  where GoodsName='零星物品')) and a.GoodsName like '%" + Condition + "%' ";
                return db.ExecuteDataset(conn, CommandType.Text, query);
            }
        }
        /// <summary>
        /// 从库存表中获取主副调食材的库存
        /// </summary>
        /// <param name="Condition"></param>
        /// <returns></returns>
        public DataSet GetCon_Goods_StocksList1(string Condition)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select distinct(GoodsName),GOODSNO from CON_Goods_Stocks_View where GoodsName like '%" + Condition + "%' ";
                return db.ExecuteDataset(conn, CommandType.Text, query);
            }
        }

        /// <summary>
        /// 获取仓库中的零星物品
        /// </summary>
        /// <param name="Condition"></param>
        /// <returns></returns>
        public DataSet GetCon_Goods_Stocks_SporadicList(string Condition)
        {
            //using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            //{
            //    sqldb db = new sqldb();
            //    string query = "select * from CON_Goods_Stocks_Sporadic_View where 1=1 " + Condition;
            //    return db.ExecuteDataset(conn, CommandType.Text, query);
            //}
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select b.ID,a.* from CON_Goods_Stocks_Sporadic_View a left join con_goods b on " +
                               "a.GOODSNO=b.GoodsNo where 1=1 " +
                               "and a.GOODSNO in( select GoodsNo from Con_Goods where PriorID " +
                               "in(select ID from Con_Goods  where GoodsName='零星物品')) and a.GoodsName like '%" + Condition + "%' ";
                return db.ExecuteDataset(conn, CommandType.Text, query);
            }
        }

        /// <summary>
        /// 根据ID获取食材名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetGoodsNameByID(int id)
        {
            sqldb db = new sqldb();
            string query = "select isnull((select GoodsName from Con_Goods where ID = " + id.ToString() + "),'') GoodsName";
            object gname = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return gname.ToString();
        }
        /// <summary>
        /// 获取出库的食材名称
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, string> GetOutGoodsName(string goodNames)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();

                string query = "select distinct(g.id),g.goodsname  from con_goods g inner join con_instock_detailed i on "
                                  + " g.id=i.goods_id where g.goodsname like '%" + goodNames + "%'";
                Dictionary<int, string> dic = new Dictionary<int, string>();
                foreach (DataRow dr in db.ExecuteDataset(conn, CommandType.Text, query).Tables[0].Rows)
                {
                    dic.Add(Convert.ToInt32(dr[0]), dr[1].ToString());
                }
                return dic;
            }
        }
        /// <summary>
        /// 根据食材id来获取所有的价格列表
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public static List<double> GetPriceListByGoodsId(int? goodsId)
        {
            if (goodsId.HasValue)
            {

                using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
                {
                    sqldb db = new sqldb();

                    string query = "select PRICE from CON_Goods_Stocks_View where STOCKS_COUNT>0 and ID=" + goodsId.Value;
                    List<double> doc = new List<double>();
                    foreach (DataRow dr in db.ExecuteDataset(conn, CommandType.Text, query).Tables[0].Rows)
                    {
                        doc.Add(Convert.ToDouble(dr[0]));
                    }
                    return doc;
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 是否存在子节点
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsExistSubNode(int id)
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Con_Goods where PriorID = " + id.ToString();
            object existcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return (Convert.ToInt32(existcount) > 0) ? true : false;
        }
    }
}
